package com.connection.second.wallpaper.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.connection.second.common.core.redis.RedisCache;
import com.connection.second.common.utils.DateUtils;
import com.connection.second.wallpaper.domain.entity.PaperClassify;
import com.connection.second.wallpaper.domain.entity.PaperWall;
import com.connection.second.wallpaper.domain.page.PaperWallRankPage;
import com.connection.second.wallpaper.mapper.PaperClassifyMapper;
import com.connection.second.wallpaper.mapper.PaperWallMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.connection.second.wallpaper.mapper.PaperWallRankMapper;
import com.connection.second.wallpaper.domain.entity.PaperWallRank;
import com.connection.second.wallpaper.service.IPaperWallRankService;

/**
 * 壁纸排名Service业务层处理
 * 
 * @author FL
 * @date 2024-11-25
 */
@Service
public class PaperWallRankServiceImpl implements IPaperWallRankService 
{
    @Autowired
    private PaperWallRankMapper paperWallRankMapper;
    @Autowired
    private PaperWallMapper paperWallMapper;
    @Autowired
    private PaperClassifyMapper paperClassifyMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询壁纸排名
     * 
     * @param id 壁纸排名主键
     * @return 壁纸排名
     */
    @Override
    public PaperWallRank selectPaperWallRankById(Long id)
    {
        return paperWallRankMapper.selectPaperWallRankById(id);
    }

    /**
     * 查询壁纸排名列表
     * 
     * @param paperWallRank 壁纸排名
     * @return 壁纸排名
     */
    @Override
    public List<PaperWallRankPage> selectPaperWallRankList(PaperWallRank paperWallRank) {
        // 构建唯一的缓存键
        String cacheKey = "selectPaperWallRankList_" + paperWallRank.getId() + "_" + paperWallRank.getRank();

        // 尝试从缓存中获取数据
        List<PaperWallRankPage> cachedList = redisCache.getCacheList(cacheKey);

        if (cachedList != null && !cachedList.isEmpty()) {
            // 如果缓存中存在数据，直接返回缓存的数据
            return cachedList;
        } else {
            // 如果缓存中没有数据，执行数据库查询
            List<PaperWallRankPage> list = new ArrayList<>();
            List<PaperWallRank> ranks = paperWallRankMapper.selectPaperWallRankList(paperWallRank);

            for (PaperWallRank rank : ranks) {
                PaperWallRankPage tmp = new PaperWallRankPage();
                BeanUtils.copyProperties(rank, tmp);
                PaperWall wall = paperWallMapper.selectPaperWallById(rank.getWallId());
                if (ObjectUtil.isNull(wall)) {
                    continue;
                }
                BeanUtils.copyProperties(wall, tmp);
                PaperClassify classify = paperClassifyMapper.selectPaperClassifyById(wall.getClassId());
                tmp.setClassName(classify.getName());
                list.add(tmp);
            }

            // 将查询结果存入缓存，并设置过期时间（例如：30分钟）
            redisCache.setCacheList(cacheKey, list);
            redisCache.expire(cacheKey, 30, TimeUnit.MINUTES);

            return list;
        }
    }

    /**
     * 新增壁纸排名
     * 
     * @param paperWallRank 壁纸排名
     * @return 结果
     */
    @Override
    public int insertPaperWallRank(PaperWallRank paperWallRank)
    {
        Long wallId = paperWallRank.getWallId();
//        壁纸必须存在
        PaperWall wall = paperWallMapper.selectPaperWallById(wallId);
        if (wall == null) {
            return 0;
        }
        return paperWallRankMapper.insertPaperWallRank(paperWallRank);
    }

    /**
     * 修改壁纸排名
     * 
     * @param paperWallRank 壁纸排名
     * @return 结果
     */
    @Override
    public int updatePaperWallRank(PaperWallRank paperWallRank)
    {
        paperWallRank.setUpdateTime(DateUtils.getNowDate());
        return paperWallRankMapper.updatePaperWallRank(paperWallRank);
    }

    /**
     * 批量删除壁纸排名
     * 
     * @param ids 需要删除的壁纸排名主键
     * @return 结果
     */
    @Override
    public int deletePaperWallRankByIds(Long[] ids)
    {
        return paperWallRankMapper.deletePaperWallRankByIds(ids);
    }

    /**
     * 删除壁纸排名信息
     * 
     * @param id 壁纸排名主键
     * @return 结果
     */
    @Override
    public int deletePaperWallRankById(Long id)
    {
        return paperWallRankMapper.deletePaperWallRankById(id);
    }
}
